package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
//import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import bsh.util.Util;

import com.vcaresolution.logistic.form.DatemanagerForm;
//import com.vcaresolution.logistic.form.JobregisterForm;
//import com.vcaresolution.logistic.pojo.Contact;
//import com.vcaresolution.logistic.pojo.Jobregister;
//import com.vcaresolution.logistic.pojo.Port;
//import com.vcaresolution.logistic.pojo.Sector;
//import com.vcaresolution.logistic.pojo.Vessel;
import com.vcaresolution.logistic.pojo.Datemanager;
import com.vcaresolution.logistic.to.DatemanagerTO;
//import com.vcaresolution.logistic.to.JobregisterTO;
import com.vcaresolution.util.Utils;

@Service
public class DatemanagerServiceImpl extends BaseService implements DatemanagerService{

	private Datemanager populateDatemanagerPojo(DatemanagerForm datemanagerForm){
		Datemanager datemanager = new Datemanager();
		BeanUtils.copyProperties(datemanagerForm, datemanager);
		
		return datemanager;
	}
	
	@Override
	@Transactional
	public void add(DatemanagerForm datemanagerForm) {
		Datemanager datemanager = populateDatemanagerPojo(datemanagerForm);
		
//		datemanager.setDmid ((getDaoFactory().getUtilityDAO().findById(Datemanager.class, datemanagerForm.getJobnoid()));
//		
//		if(!Utils.isEmptyInteger(datemanagerForm.getJobnoid())){
//			jobregister.setVesselByBkgvslid(getDaoFactory().getUtilityDAO().findById(Vessel.class, jobregisterForm.getBkgvslid()));
//		}

		getDaoFactory().getUtilityDAO().add(datemanager);
	}

	@Override
	@Transactional
	public void update(DatemanagerForm datemanagerForm) {
		Datemanager datemanager= populateDatemanagerPojo(datemanagerForm);
		
//		datemanager.setJoid(getDaoFactory().getUtilityDAO().findById(Port.class, jobregisterForm.getPodid()));
//		
//		if(!Utils.isEmptyInteger(datemanagerForm.getBkgvslid())){
//			datemanager.setBkgvslid(getDaoFactory().getUtilityDAO().findById[Jobregister].class, jobregisterForm.getBkgvslid()));
//		}
//		
//		datemanager.setContactByShipperid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getShipperid()));
//		
		getDaoFactory().getUtilityDAO().update(datemanager);
	}

	@Override
	@Transactional
	public void delete(DatemanagerForm datemeanagerForm) {
		getDaoFactory().getUtilityDAO().delete(populateDatemanagerPojo(datemeanagerForm));
	}

	@Override
	@Transactional
	public DatemanagerTO findById(Serializable datemanagerID) {
		Datemanager datemanager = (Datemanager) getDaoFactory().getUtilityDAO().findById(Datemanager.class,datemanagerID);
		
		DatemanagerTO datemanagerTO = new DatemanagerTO();
		BeanUtils.copyProperties(datemanager, datemanagerTO);
		
		if(datemanager.getJobregister() != null){
			if(!Utils.isEmptyInteger(datemanager.getJobregister().getJobnoid())){
				datemanagerTO.setJobnoid(datemanager.getJobregister().getJobnoid());
			}
		}
		datemanagerTO.setJobnoid(datemanager.getJobregister().getJobnoid());
				
//		if(datema.getVesselByBkgvslid() != null){
//			if(!Utils.isEmptyInteger(jobregister.getVesselByBkgvslid().getVesselid())){
//				jobregisterTO.setBkgvslid(jobregister.getVesselByBkgvslid().getVesselid());
//			}
//		}
		return datemanagerTO; 
	}

	@Override
	@Transactional
	public List<DatemanagerTO> findByAll() {
		List<Datemanager> datemanagerList = getDaoFactory().getUtilityDAO().findByAll(Datemanager.class);
		
		List<DatemanagerTO> datemanagerFormList = new ArrayList<DatemanagerTO>();
		
		if( datemanagerList != null && !datemanagerList.isEmpty() ){
			DatemanagerTO datemanagerForm = null;
			for(Datemanager datemanager: datemanagerList){
				datemanagerForm = new DatemanagerTO();
				
				BeanUtils.copyProperties(datemanager, datemanagerForm);
				if(datemanager.getAuditdate() != null){
					datemanagerForm.setAuditDateToString(Utils.convertDateObjectToDDMMYYYY(datemanager.getAuditdate()));
				}
				datemanagerFormList.add(datemanagerForm);
			}
		}	
		return datemanagerFormList;
	}
}